package cn.agiledata.bank.linkbank.impl.cncb;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;

import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.common.util.XmlUtil;
import cn.agiledata.bank.linkbank.exception.LinkBankException;
import cn.agiledata.bank.linkbank.impl.AbstractBankPayResponse;

/**
 * 中国中信银行网上银行支付响应
 * 
 * @author 马贤波
 * 
 */
public final class CncbNetPayResponse extends AbstractBankPayResponse {

    protected String[] getValidKeys() {
        return new String[] { CncbNetConstant.SIGN_MSG };
    }

    public boolean isAmountValidatable() {
        return true;
    }

    /**
     * 从返回信息中分割出有效信息
     * 
     * @param message
     *            返回信息
     * @return 有效信息
     */
    private String getValidMessage(String message) {
        String[] strings = message.split(CncbNetConstant.MESSAGE_SPLIT_SYMBOL);
        return strings[1];
    }

    private void parseMessage(String message) throws LinkBankException {
        try {
            Document doc = XmlUtil.createDocument(message);
            Element root = doc.getRootElement();
            this.setMessage(CncbNetConstant.AMOUNT, root
                    .elementTextTrim(CncbNetConstant.AMOUNT)); // 金额
            this.setMessage(CncbNetConstant.ORDER_DATE, root
                    .elementTextTrim(CncbNetConstant.ORDER_DATE)); // 订单时间
            this.setMessage(CncbNetConstant.ORDER_NO, root
                    .elementTextTrim(CncbNetConstant.ORDER_NO)); // 订单号
            this.setMessage(CncbNetConstant.STATUS_ID, root
                    .elementTextTrim(CncbNetConstant.STATUS_ID)); // 银行返回码
        } catch (Exception e) {
            throw LinkBankException.PARSE_RESPONSE_ERROR;
        }
    }

    public void setMessages(Map messages) throws LinkBankException {
        String message = (String) messages.get(CncbNetConstant.SIGN_MSG);
        message = getValidMessage(message);
        parseMessage(message);
    }

    protected AbstractBankPayResponse getCopiedInstance() {
        return new CncbNetPayResponse();
    }

    /**
     * 金额
     */
    public BigDecimal getAmount() {
        try {
            return new BigDecimal(this.getMessage(CncbNetConstant.AMOUNT));
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 
     * @return 订单时间
     */
    public Date getDate() {
        String dateValue = getMessage(CncbNetConstant.ORDER_DATE);
        return DateUtil.string2Date(dateValue,
                CncbNetConstant.ORDER_DATE_FORMAT);
    }

    /**
     * 
     * @return 订单号
     */
    public String getOrderId() {
        return this.getMessage(CncbNetConstant.ORDER_NO);
    }

    /**
     * 
     * @return 银行返回码
     */
    public String getReturnCode() {
        return this.getMessage(CncbNetConstant.STATUS_ID);
    }

    /**
     * 
     * @return 银行返回错误信息
     */
    public String getReturnMessage() {
        return null;
    }

    /**
     * 
     * @return 银行返回正确码
     */
    public String getSuccessReturnCode() {
        return CncbNetConstant.SUCCESS;
    }
}
